<?php
namespace Common\Model;
use Home\Model\GoodsModel;
use Think\Model;

class StorePageModel extends Model  {

    use ActivityModel;

    protected $tableName = 'store_page';

    /**
     * 马上买
     * @param $local_id
     * @return string
     */
    public function maShangMai($local_id){
        $value = $this->cache(true,86400, 'redis')->field('value')->where(['local_id' => $local_id, 'type' => '马上买'])->find();
        if (empty($value)) {
            return '';
        }
        $arr = explode(',', $value['value']);
        $arr = implode(',', $arr);
        $where['a.store_pid'] = ['in', $arr];
        $where['a.store_status'] = 'true';
        $where['a.local_id'] = $local_id;

        $res =  M('b2c_store_product a')->cache(true,86400, 'redis')->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id and b.disabled = "false"', 'left')
            ->field("a.*,b.name,b.title,b.spec_info")
            ->where($where)
            ->select();

        //商品促销价格查询
        $this->GoodsPromotion($res, $local_id);


        $GoodsModel = new GoodsModel();
        foreach ($res as $k=>$v) {
            // 判断闪购
            $flashGoods = $GoodsModel->checkGoodsIsStarBuy($v['store_pid']);
            if ($flashGoods) {
                $res[$k]['flashGoods'] = $flashGoods;
                $productMsg[$k]['store'] = $flashGoods['initnum'];
                $productMsg[$k]['freez'] = 0;
            }
            // 判断组合促销
            $checkProductIsGroupPromotion = checkProductIsGroupPromotion($v['store_pid'], $v['local_id']);
            $res[$k]['groupPromotion'] = $checkProductIsGroupPromotion;

        }

//        echo M('b2c_store_product a')->_sql();
        return $res;
    }
    /**
     * 推荐
     * @param $local_id
     * @return string
     */
    public function tuiJian($local_id){
        $value = $this->cache(true,86400, 'redis')->field('value')->where(['local_id' => $local_id, 'type' => '推荐'])->find();
        if (empty($value)) {
            return '';
        }
        $arr = explode(',', $value['value']);
        $arr = implode(',', $arr);
        $where['a.store_pid'] = ['in', $arr];
        $where['a.store_status'] = 'true';
        $where['a.local_id'] = $local_id;

        $res = M('b2c_store_product a')->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id and b.disabled = "false"', 'left')
            ->field("a.*,b.name,b.spec_info")
            ->where($where)
            ->select();
        //商品促销价格查询
        $this->GoodsPromotion($res, $local_id);

        $GoodsModel = new GoodsModel();
        foreach ($res as $k=>$v) {
            // 判断闪购
            $flashGoods = $GoodsModel->checkGoodsIsStarBuy($v['store_pid']);
            if ($flashGoods) {
                $res[$k]['flashGoods'] = $flashGoods;
                $res[$k]['store'] = $flashGoods['initnum'];
                $res[$k]['freez'] = 0;
            }
            // 判断组合促销
            $checkProductIsGroupPromotion = checkProductIsGroupPromotion($v['store_pid'], $v['local_id']);
            $res[$k]['groupPromotion'] = $checkProductIsGroupPromotion;

        }
        return $res;
    }

}